﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;

namespace System.Web.Mvc
{
	public enum ListConstraintScope
	{
		In,
		NotIn
	}

	public class ListConstraint : IRouteConstraint
	{
		private ListConstraint(ListConstraintScope scope, IEnumerable<string> values)
		{
			Values = values;
			Scope = scope;
		}

		public static ListConstraint Create(params string[] values)
		{
			return new ListConstraint(ListConstraintScope.In, values);
		}

		public static ListConstraint Create(IEnumerable<string> values)
		{
			return new ListConstraint(ListConstraintScope.In, values);
		}

		public static ListConstraint Create(ListConstraintScope scope, params string[] values)
		{
			return new ListConstraint(scope, values);
		}

		public static ListConstraint Create(ListConstraintScope scope, IEnumerable<string> values)
		{
			return new ListConstraint(scope, values);
		}

		public ListConstraintScope Scope { get; set; }
		public IEnumerable<string> Values { get; set; } 

		public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
		{
			string value = values[parameterName].ToString();

			if (Scope == ListConstraintScope.In)
				return Values.Contains(value, StringComparer.InvariantCultureIgnoreCase);
			else
				return !Values.Contains(value, StringComparer.InvariantCultureIgnoreCase);
		}
	}
}